/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.can.bc;

import edu.upn.sigemfi.can.be.EmpresaCliente;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Jhosep
 */
public class EmpresaClienteDelegate {

    private EmpresaClienteFacadeLocal local;

    public EmpresaClienteDelegate() {
        local = lookupEmpresaClienteFacade();
    }

    public void crear(EmpresaCliente empresaCliente) throws Exception {
        try {
            local.create(empresaCliente);
        } catch (Exception e) {
            local = lookupEmpresaClienteFacade();
            throw new Exception("Crear Empresa Cliente" + e.getMessage(), e);
        }
    }

    public void editar(EmpresaCliente empresaCliente) throws Exception {
        try {
            local.edit(empresaCliente);
        } catch (Exception e) {
            local = lookupEmpresaClienteFacade();
            throw new Exception("Editar Empresa Cliente" + e.getMessage(), e);
        }
    }

    public void eliminar(EmpresaCliente empresaCliente) throws Exception {
        try {
            local.remove(empresaCliente);
        } catch (Exception e) {
            local = lookupEmpresaClienteFacade();
            throw new Exception("Eliminar Empresa Cliente" + e.getMessage(), e);
        }
    }

    public EmpresaCliente buscar(long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupEmpresaClienteFacade();
            throw new Exception("Buscar Empresa Cliente" + e.getMessage(), e);
        }
    }

    public List<EmpresaCliente> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupEmpresaClienteFacade();
            throw new Exception("Listar Empresa Cliente. " + e.getMessage(), e);
        }
    }

    private EmpresaClienteFacadeLocal lookupEmpresaClienteFacade() {
        try {
            Context c = new InitialContext();
            return (EmpresaClienteFacadeLocal) c.lookup("java:comp/env/EmpresaClienteFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
